function drawPrize(prizes) {
  const random = Math.random();
  let cumulativeProbability = 0;
  let datas = []
  prizes.forEach(item => {
    if(item.nums > 0) {
      datas.push(item)
    }
  })
  datas.sort((a,b) => b.probability - a.probability)
  for (const prize of prizes) {
      cumulativeProbability += prize.probability;
      if (random < cumulativeProbability) {
          if (prize.nums <= 0 && datas[0]){
            return datas[0]
          } else {
            return prize;
          }
      }
  }
  
}

module.exports =  {
  drawPrize
}